from django.db import models
from user.models import User
from utils.models import BaseModel
# Create your models here.

class SystemMen(BaseModel):
    '''系统菜单'''
    menuId = models.BigAutoField(primary_key=True, verbose_name='菜单ID',help_text='菜单ID')
    menuName = models.CharField(max_length=50,verbose_name='菜单名称',help_text='菜单名称')
    # parentName = models.CharField(max_length=50,verbose_name='父级菜单名称',help_text='父级菜单名称',default='')
    menuType = models.CharField(max_length=50,verbose_name='菜单类型',help_text='菜单类型',default='M')
    parentId = models.IntegerField(verbose_name='父菜单ID',help_text='父菜单ID',null=True, blank=True)
    # parentId = models.ForeignKey('self', on_delete=models.SET_NULL, related_name='subs', null=True, blank=True, verbose_name='父菜单ID')
    orderNum = models.IntegerField(verbose_name='显示顺序',help_text='显示顺序')
    path = models.CharField(max_length=200,verbose_name='路由地址',help_text='路由地址',blank=True,default='')
    component = models.CharField(max_length=200,verbose_name='组件路径',help_text='组件路径',blank=True,default='')
    icon = models.CharField(max_length=200,verbose_name='菜单图标',help_text='菜单图标',blank=True,default='')
    perms = models.CharField(max_length=200, verbose_name='权限标识', help_text='权限标识',blank=True,default='')
    status = models.IntegerField(verbose_name='菜单状态（0正常 1停用）',help_text='菜单状态（0正常 1停用）',default='')

    class Meta:
        db_table = 'sys_menu'
        verbose_name ="系统菜单"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.menuName


class SystemRole(BaseModel):
    '''
    系统角色
    '''
    role_id = models.BigAutoField(primary_key=True, verbose_name='角色id', help_text='角色id')
    role_name = models.CharField(max_length=50,verbose_name='角色名称',help_text='角色名称')
    role_key = models.CharField(max_length=50,verbose_name='角色权限字符串',help_text='角色权限字符串')
    role_sort = models.IntegerField(verbose_name='显示顺序',help_text='显示顺序')
    data_scope = models.CharField(max_length=1, default='1',
                                  verbose_name='数据范围（1：全部数据权限 2：自定数据权限 3：本部门数据权限 4：本部门及以下数据权限）')
    menu_check_strictly = models.BooleanField(default=True, verbose_name='菜单树选择项是否关联显示')
    dept_check_strictly = models.BooleanField(default=True, verbose_name='部门树选择项是否关联显示')
    status = models.CharField(max_length=1, verbose_name='角色状态（0正常 1停用）')
    del_flag = models.CharField(max_length=1, default='0', verbose_name='删除标志（0代表存在 2代表删除）')


    class Meta:
        db_table = 'sys_role'
        verbose_name = '角色信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.role_name



class SystemUserRole(models.Model):
    '''
    系统用户和角色关联表
    '''
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    role = models.ForeignKey(SystemRole,on_delete=models.CASCADE)

    class Meta:
        db_table = 'sys_user_role'
        verbose_name = '系统用户和角色关联表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.user_id)



class SystemRoleMenu(models.Model):
    '''
    系统用角色和菜单关联表
    '''
    role = models.ForeignKey(SystemRole, on_delete=models.CASCADE)
    menu = models.ForeignKey(SystemMen,on_delete=models.CASCADE)

    class Meta:
        db_table = 'sys_role_menu'
        verbose_name = '系统用角色和菜单关联表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.role_id)


class SysOperLog(models.Model):
    '''
    操作日志记录
    '''
    oper_id = models.BigAutoField(primary_key=True, verbose_name='日志主键')
    title = models.CharField(max_length=50, default='', verbose_name='模块标题')
    business_type = models.IntegerField(default=0, verbose_name='业务类型（0其它 1新增 2修改 3删除）')
    method = models.CharField(max_length=100, default='', verbose_name='方法名称')
    request_method = models.CharField(max_length=10, default='', verbose_name='请求方式')
    # operator_type = models.IntegerField(default=0, verbose_name='操作类别（0其它 1后台用户 2手机端用户）')
    oper_name = models.CharField(max_length=50, default='', verbose_name='操作人员')
    # dept_name = models.CharField(max_length=50, default='', verbose_name='部门名称')
    oper_url = models.CharField(max_length=255, default='', verbose_name='请求URL')
    oper_ip = models.CharField(max_length=128, default='', verbose_name='主机地址')
    oper_location = models.CharField(max_length=255, default='', verbose_name='操作地点')
    oper_param = models.CharField(max_length=2000, default='', verbose_name='请求参数')
    json_result = models.CharField(max_length=2000, default='', verbose_name='返回参数')
    status = models.IntegerField(default=0, verbose_name='操作状态（0正常 1异常）')
    error_msg = models.CharField(max_length=2000, default='', verbose_name='错误消息')
    oper_time = models.DateTimeField(auto_now_add=True, verbose_name='操作时间')

    class Meta:
        db_table = 'sys_oper_log'
        verbose_name = '操作日志记录'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.oper_id)


class SysLoginInfo(models.Model):
    info_id = models.BigAutoField(primary_key=True, verbose_name='访问ID')
    user_name = models.CharField(max_length=50, default='', verbose_name='用户账号')
    ipaddr = models.CharField(max_length=128, default='', verbose_name='登录IP地址')
    login_location = models.CharField(max_length=255, default='', verbose_name='登录地点')
    browser = models.CharField(max_length=50, default='', verbose_name='浏览器类型')
    os = models.CharField(max_length=50, default='', verbose_name='操作系统')
    status = models.CharField(max_length=1, default='0', verbose_name='登录状态（0成功 1失败）')
    msg = models.CharField(max_length=255, default='', verbose_name='提示消息')
    login_time = models.DateTimeField(auto_now_add=True)
    # login_time = models.DateTimeField(null=True, verbose_name='访问时间')

    class Meta:
        db_table = 'sys_logininfor'
        verbose_name = '系统访问记录'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.info_id)


class SysJob(BaseModel):
    job_id = models.BigAutoField(primary_key=True, verbose_name='任务ID')
    job_name = models.CharField(max_length=64, default='', verbose_name='任务名称')
    job_group = models.CharField(max_length=64, default='DEFAULT', verbose_name='任务组名')
    invoke_target = models.CharField(max_length=500, verbose_name='调用目标字符串')
    cron_expression = models.CharField(max_length=255, default='', verbose_name='cron执行表达式')
    misfire_policy = models.CharField(max_length=20, default='3', verbose_name='计划执行错误策略（1立即执行 2执行一次 3放弃执行）')
    concurrent = models.CharField(max_length=1, default='1', verbose_name='是否并发执行（0允许 1禁止）')
    status = models.CharField(max_length=1, default='0', verbose_name='状态（0正常 1暂停）')


    class Meta:
        db_table = 'sys_job'
        verbose_name = '定时任务调度表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.job_id)


class SysJobLog(models.Model):
    job_log_id = models.BigAutoField(primary_key=True, verbose_name='任务日志ID')
    job_name = models.CharField(max_length=64, verbose_name='任务名称')
    job_group = models.CharField(max_length=64, verbose_name='任务组名')
    invoke_target = models.CharField(max_length=500, verbose_name='调用目标字符串')
    job_message = models.CharField(max_length=500, null=True, blank=True, verbose_name='日志信息')
    status = models.CharField(max_length=1, default='0', verbose_name='执行状态（0正常 1失败）')
    exception_info = models.CharField(max_length=2000, default='', verbose_name='异常信息')
    create_time = models.DateTimeField(null=True, verbose_name='创建时间')

    class Meta:
        db_table = 'sys_job_log'
        verbose_name = '定时任务调度日志表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.job_log_id)